package com.android.project.games;

import javax.microedition.khronos.opengles.GL10;

import android.util.Log;

import com.android.project.framework.gl.Camera2D;
import com.android.project.framework.gl.SpriteBatcher;
import com.android.project.framework.impl.GLGraphics;



public class WorldRenderer2 {
	//static final float FRUSTUM_WIDTH = 10;
	//static final float FRUSTUM_HEIGHT = 15;
	static final float FRUSTUM_WIDTH = 320;
	static final float FRUSTUM_HEIGHT = 480;
	World2 world2;
	SpriteBatcher batcher;
	GLGraphics glGraphics;
	Camera2D guiCam;


	
	public WorldRenderer2( GLGraphics glGraphics,SpriteBatcher batcher, World2 world2){
		this.glGraphics = glGraphics;
		this.batcher =  batcher;
		this.world2 = world2;
		this.guiCam = new Camera2D(glGraphics, FRUSTUM_WIDTH, FRUSTUM_HEIGHT);
		
	}
	
	public void render(String s) {
		// TODO Auto-generated method stub
		//Log.d("guiCam position", "" +guiCam.position.y );
		if(!world2.getLabels().isEmpty()){
			//Log.d("Render", " " + world2.render);
			if(!world2.keyboardShow || world2.render == 0){
				guiCam.position.y = FRUSTUM_HEIGHT/2;
			}
			else if(world2.keyboardShow && world2.render == 1){
				guiCam.position.y = world2.getLabels().get(0).position.y - 40;
				
			}
			else if(world2.keyboardShow && world2.render == 2){
				guiCam.position.y = guiCam.position.y + 32;
			}
			else if(world2.keyboardShow && world2.render == 3){
				guiCam.position.y = guiCam.position.y;
			}
 
		}	
		guiCam.setViewportAndMatrices();
		renderBackground();
		renderObjects(s);
		
	}

	private void renderBackground() {
		// TODO Auto-generated method stub
		batcher.beginBatch(Assets.background);
		batcher.drawSprite(guiCam.position.x, guiCam.position.y, FRUSTUM_WIDTH, FRUSTUM_HEIGHT, Assets.backgroundRegion);
		batcher.endBatch();
		
	}

	private void renderObjects(String s) {
		// TODO Auto-generated method stub
		GL10 gl = glGraphics.getGl();
		gl.glEnable(GL10.GL_BLEND);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
		if(world2.getLabels().size() > 0 ){
			batcher.beginBatch(Assets.items);
			renderLabels(s);	
			batcher.endBatch();
		}
		
		gl.glDisable(GL10.GL_BLEND);
		
	}

	private void renderLabels(String s) {
		// TODO Auto-generated method stub
		int len = world2.labels.size();
	    for(int i = 0; i < len; i++){
			Label label = world2.getLabels().get(i);
			
			if(label.state == Label.LABEL_STATE_INPUT){
				batcher.drawSprite(label.position.x, label.position.y , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelRegion);
				Assets.font.drawText(batcher, label.word, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y  );
				
				
			}
			if (label.state == Label.LABEL_STATE_INSERT ){
				batcher.drawSprite(label.position.x, label.position.y , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelInsertRegion);
				if(label.writeEnable){
					Assets.font.drawText(batcher, s, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
					label.word = s;
				}
					
				else
					Assets.font.drawText(batcher, "", label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
			}
			if(label.state == Label.LABEL_STATE_RIGHT){
				batcher.drawSprite(label.position.x, label.position.y  , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelRightRegion);
				Assets.font.drawText(batcher, label.word, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
			}
			if(label.state == Label.LABEL_STATE_WRONG){
				batcher.drawSprite(label.position.x, label.position.y , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelWrongRegion);
				Assets.font.drawText(batcher, label.word, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
			}
			if(label.state == Label.LABEL_STATE_SEMI){
				batcher.drawSprite(label.position.x, label.position.y , Label.LABEL_WIDTH, Label.LABEL_HEIGHT, Assets.labelSemiRegion);
				Assets.font.drawText(batcher, label.word, label.position.x - Label.LABEL_WIDTH/2 + 15, label.position.y );
			}
			
		}
		
	}

}
